package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class WechatServiceImpl implements WechatService {
    //小程序登录
    private static final String REQUEST_URL  = "https://api.weixin.qq.com/sns/jscode2session?authorization_code";
    //获取手机号
    private static final String PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
    //获取接口调用凭据
    private static final String ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    //配置文件
    @Value("${wechat.appId}")
    private String appId;
    @Value("${wechat.appSecret}")
    private String appSecret;



    /**
     * 获取opnid
     *
     * @param code
     * @return
     */
    @Override
    public String getOpenid(String code) {
        //准备请求参数
        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("appid",appId);
        stringObjectMap.put("secret",appSecret);
        stringObjectMap.put("js_code",code);

        //准备请求路径
        String httpcode= HttpUtil.get(REQUEST_URL,stringObjectMap);
        //解析返回的JSON
        JSONObject jsonObject = JSONUtil.parseObj(httpcode);
        //判断错误码又没值
        if(ObjectUtil.isNotEmpty(jsonObject.getInt("errcode"))){
        throw new BaseException(jsonObject.getStr("errmsg"));
        }
        return jsonObject.getStr("openid");
    }


    /**
     * 获取接口调用凭据
     */
      public String getAccessToken(){
            //k-v值
          HashMap<String, Object> stringObjectHashMap = new HashMap<>();
          stringObjectHashMap.put("appid",appId);
          stringObjectHashMap.put("secret",appSecret);

          String accesstoken = HttpUtil.get(ACCESS_TOKEN, stringObjectHashMap);
          JSONObject entries = JSONUtil.parseObj(accesstoken);

          if(ObjectUtil.isNotNull(entries.getInt("errcode"))){
              throw new BaseException("错误");
          }

          return entries.getStr("access_token");
      }




    /**
     * 获取手机号
     *
     * @param detailCode
     * @return
     */
    @Override
    public String getPhone(String detailCode) {
        String accessToken = getAccessToken();
        //k-v对
       HashMap<String, Object> stringObjectHashMap = new HashMap<>();
       stringObjectHashMap.put("code",detailCode);

        String post = HttpUtil.post(PHONE_URL+accessToken, JSONUtil.toJsonStr(stringObjectHashMap));
        JSONObject entries = JSONUtil.parseObj(post);
        if (!(entries.getInt("errcode")==0)){
            throw new BaseException("错误");
        }

        return entries.getJSONObject("phone_info").getStr("phoneNumber");
    }
}
